$(document).ready(function() {
	// var folderNames = new Array;
	var sources = new Array;
	var path = new Array;

	var tabs = new Array;
	tabs.push("\t");

	var txt = "";
	/*
 var count = 0;
 function each(i, field) {
 var type = $.type(field);
 var tab = tabs.join("|");
 if (type == "object") {
 parse_object(field);
 }
 else if (type == "array") {
 txt += tab + i + " " + "type:" + type + "<br/>";
 tabs.push("\t");
 $(field).each(each);
 tabs.pop();
 }
 else {
 txt += tab + i + ":" + field + "<br/>";
 }
 }
 */

	function parse(rootObject) {
		$.each(rootObject.roots, function(i, field) {
			parse_object(field);
		});
	}

	function parse_array(array) {
		tabs.push("\t");
		txt += "<div class='children'>";
		for(var i = 0; i < array.length; i++) {
			var object = array[i];
			parse_object(object);
		};
		txt += "</div>";
		tabs.pop();
	}

	function parse_object(object) {
		var tab = tabs.join("|---");
		txt += "<div>";
		if(object.type == "folder") {
			if($.type(object.children) == "array") {
				// txt += "<div>";
				txt += "<div onclick='toggleChildren(this)'>";
				txt += tab;
				txt += "<img id='closed' src='images\\folder_closed.png' alt=''/>";
				txt += "<img id='open' style='display: none;' src='images\\folder_open.png' alt=''/>";
				txt += object.name;
				txt += "</div>";
				// console.log(object.name);
				path.push(object.name);
				parse_array(object.children);
				path.pop();
				// txt += "</div>";
			} else {
				console.log("unexcepted");
			}
		} else if(object.type == "url") {
			txt += tab;
			txt += "<img class='favicon' src='http://www.google.cn/favicon.ico' alt=''/>";
			// txt += "<a href='";
			txt += "<a href='";
			txt += object.url;
			txt += "'>"
			txt += object.name;
			// console.log(path.join("/"));
			txt += "|---(" + "路径：" + path.join("/") + ")";
			txt += "|---(" + "网站：" + analyse_item(object) + ")";
			txt += "</a>";
			/*
		object.path = path.slice(0);
		var matchResult = object.url.match(/http:\/\/.*?\//);
		if (null != matchResult) {
			sources.push(matchResult[0]);
		}
		bookmarks.push(object);
		*/
		} else {
			console.log("unexcepted");
		}
		txt += "</div>";
	}

	function analyse_urls() {

	}

	function analyse_tags() {
		// bookmarks
	}

	function analyse_sources() {
		sources.sort();
		var index = 0;
		var count = 0;
		var stub = [0];
		while(1 + index < sources.length) {
			if(stub == [1 + count + index]) {
				++count;
			} else {
				sources.splice(index, count);
				count = 0;
				var stub = [++index];
			}
		}
	}

	var rooturis = new Object;
	rooturis.normal = new Object;
	rooturis.secure = new Object;
	rooturis.count = 0;

	function analyse_item(item) {
		var uri = item.url;
		var rooturi = uri.match(/http:\/\/.*?\//);

		/*
	var array = null;
	if(undefined != rooturi){
		array = rooturis.normal[rooturi];
	}
	*/
		if(undefined == rooturi) {
			var rooturi = uri.match(/https:\/\/.*?\//);
		}
		if(undefined != rooturi) {
			var array = rooturis[rooturi];
			if(undefined == array) {
				array = [];
				rooturis[rooturi] = array;
				rooturis.count++;
			}
			array.push(uri);
		}
		return rooturi;
	}

	function toggleChildren(element) {
		$(element).next(".children").toggle();
		$(element).find("#closed").toggle();
		$(element).find("#open").toggle();
		// $(this #open).toggle();
	}

	$.getJSON("Bookmarks.txt", function(result) {
		parse(result);
		$("div#contents-folder").html(txt);
	});
});